package cn.cqsw.controller;

import cn.cqsw.pojo.Employee;
import cn.cqsw.service.EmployeeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/employee")
public class EmployeeServlet extends BaseServlet {
    //查询所有员工信息
    public void findEmployees(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Employee> employees = new EmployeeService().findEmployees();
        req.setAttribute("employees",employees);
        req.getRequestDispatcher("/employeeList.jsp").forward(req,resp);
    }
    //删除的操作
    public void deleteEmployee(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if(id != null){
            int result = new EmployeeService().deleteUserById(new Integer(id));
            if(result==1){
                resp.sendRedirect("/employee?method=findEmployees");
            }else{
                resp.getWriter().write("delete failed.....");
            }
        }
    }
    public void addEmployee(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("ename");
        String birthday = req.getParameter("birthday");
        String gender = req.getParameter("gender");
        String jobs = req.getParameter("jobs");
        String phone = req.getParameter("phone");
        Employee employee=new Employee();
        employee.setName(name);
        employee.setBirthday(birthday);
        employee.setGender(gender);
        employee.setJobs(jobs);
        employee.setPhone(phone);
        int result = new EmployeeService().insertEmployee(employee);
        if(result==1){
            resp.sendRedirect("/employee?method=findEmployees");
        }else{
            resp.getWriter().write("insert failed.....");
        }
    }
    //跳转到修改用户页面
    public void updateEmployeePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Employee employee = new EmployeeService().findEmployeeById(new Integer(id));
        req.setAttribute("employee",employee);
        req.getRequestDispatcher("updateEmployee.jsp").forward(req,resp);
    }
    //修改员工的方法
    public void updateEmployee(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("ename");
        String birthday = req.getParameter("birthday");
        String gender = req.getParameter("gender");
        String jobs = req.getParameter("jobs");
        String phone = req.getParameter("phone");
        Employee employee=new Employee();
        employee.setName(name);
        employee.setBirthday(birthday);
        employee.setGender(gender);
        employee.setJobs(jobs);
        employee.setPhone(phone);
        employee.setId(new Integer(id));
        int result = new EmployeeService().updateEmployee(employee);
        if(result==1){
            resp.sendRedirect("/employee?method=findEmployees");
        }else{
            resp.getWriter().write("update failed.....");
        }
    }
}
